package utils.UI.JFreeChart;

import java.awt.RenderingHints;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.FastScatterPlot;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class UI extends ApplicationFrame{
   
	private static final long serialVersionUID = 1L;
	
    public UI(PlotHeaders headers, float[][] data) {

        super(headers.getTitle());
        
        float[][] dataPlot=populateData(data);
        
        final NumberAxis domainAxis = new NumberAxis(headers.getXlabel());
        domainAxis.setAutoRangeIncludesZero(false);
        
        final NumberAxis rangeAxis = new NumberAxis(headers.getYlabel());
        rangeAxis.setAutoRangeIncludesZero(false);
        
        final FastScatterPlot plot = new FastScatterPlot(dataPlot, domainAxis, rangeAxis);
        final JFreeChart chart = new JFreeChart(headers.getFunctionName(), plot);

        chart.getRenderingHints().put
            (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        final ChartPanel panel = new ChartPanel(chart, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        
        panel.setMinimumDrawHeight(10);
        panel.setMaximumDrawHeight(2000);
        panel.setMinimumDrawWidth(20);
        panel.setMaximumDrawWidth(2000);
        
        setContentPane(panel);

    }
  
    /**
     * Populates the data array with random values.
     */
    private float[][] populateData(float[][] data) {

    	final float step = 4.0f / (float)data[0].length;
    	float [][]dataPlot= new float[2][data[0].length];
    	
    	int p=0;
        for (int i = 0; i < data[0].length; i++) {
            dataPlot[0][i] = step*i;
            dataPlot[1][i] = 3f;
        }
        
        for (int i=0; i < data[0].length; i++) {
        	for( int j=0; j<dataPlot[0].length; j++){
	            if( Math.abs( data[0][i] - dataPlot[0][j]) < step ){
	            	dataPlot[1][j] = data[1][i];
	            }
	        }
        }
        return dataPlot;
    }

    public static void plot(PlotHeaders headers, float [][]data ) {

        final UI ui = new UI(headers, data);
        ui.pack();
        RefineryUtilities.centerFrameOnScreen(ui);
        ui.setVisible(true);

    }

}
